package com.example.demo.file.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.annotation.ApiDesc;
import com.example.demo.entity.FileUpReturnEntity;
import com.example.demo.entity.base.ResponseEntity;
import com.example.demo.file.FileManager;
import com.example.demo.file.FileManagerFactory;
import com.example.demo.util.PropertiesUtil;

@RestController
@RequestMapping(value = "/file")
public class FileController {
	
	@ApiDesc("读取文件")
	@RequestMapping(value="/readfile")
	public void readFile(HttpServletRequest request,HttpServletResponse response,
								@RequestParam String saveType,
								@RequestParam String fileUrl) throws Exception {
		
		FileManager fileManager = FileManagerFactory.build(saveType);
		
		fileManager.readFileFromRequest(response, fileUrl);
	}
	
	@ApiDesc("上传文件")
	@PostMapping(value="/uploadsinglefile")
	public Object uploadSingleFile(HttpServletRequest request,HttpServletResponse response,
										@RequestParam MultipartFile file,
										@RequestParam String module,
										@RequestParam(required = false,defaultValue = "0")boolean dontChangeName,
										@RequestParam(required = false,defaultValue = "0")boolean dontCreateDateDir) throws Exception {
		ResponseEntity<FileUpReturnEntity> ret = new ResponseEntity<>();
		
		FileManager fileManager = FileManagerFactory.build(PropertiesUtil.getProperty("fileSaveType"));
		
		FileUpReturnEntity fure = fileManager.uploadSingleFile(file, module, dontChangeName, dontCreateDateDir);
		
		return ret.setObject(fure);
	}
}
